// 上传 文章图片 接口
const db = require('../db')

exports.post = (req, res) => {
  const { id, getdate, imgurl, content } = req.body // 将传递进来的数据保存
  // console.log(imgurl)
  // console.log(id, content)

  const selectArticleIdSql = `select articleid from article where content="${content}" and date="${getdate}" and id="${id}" `

  db.query(selectArticleIdSql, (err, data) => {
    if (err) return console.log('selectArticleIdSql错误:' + err.sqlMessage)
    if (data.length < 1) return console.log('没有该文章')
    // console.log(data[0].articleid)
    const articleid = data[0].articleid
    // console.log(articleid)
    const updateArticleSql = `update article set imgurl='${imgurl}' where  articleid="${articleid}"`

    db.query(updateArticleSql, (err, data) => {
      if (err) {
        // 数据库语句执行失败
        return console.log('selectIdSql错误:' + err.sqlMessage)
      } else if (data.length < 1) {
        // 返回的值为空数组
        res.send({ mes: '添加图片失败', status: 401 })
      } else {
        res.send({ data: data, mes: '添加日记成功', status: 201 })
        // res.send({ mes: '添加图片成功', status: 201 })
        // const { data } = data
        // console.log(data)
      }
    })
  })
}
